﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _13.excel
{
    class Program
    {
        //https://www.cnblogs.com/a-zx/articles/1771211.html
        static void Main(string[] args)
        {
            //string filename = @"d:\库存数据模板.xls";
            //string connStr = "Provider =Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";

            string filename = @"d:\销售数据模板.xlsx";
            string connStr = "Provider =Microsoft.Jet.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\"";
            var conn = new OleDbConnection(connStr);
            conn.Open();//

            string sql = "select * from [Sheet1$]";//$不要修改，固定的
            var adapter = new OleDbDataAdapter(sql, conn);

            DataSet ds = new DataSet(); //存放DataTable的
            adapter.Fill(ds);//把查询的结果填充到dataset里

            conn.Close();//

            //获取数据
            DataTableCollection tbls = ds.Tables;
            DataTable tbl = tbls[0];
            //
            foreach (DataRow row in tbl.Rows)
            {
                Console.WriteLine(row[1]);
            }

            Console.WriteLine("Count: " + tbl.Rows.Count);
            Console.ReadKey();
        }
    }
}
